﻿using System;
using System.Globalization;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common.Utilities;


namespace DotNetNuke.Modules.UserDefinedTable
{


  public class EditDate : EditControl
  {


    protected HyperLink ctlHyp;
    protected WebControl ctlValueBox;



    //public EditDate()
    //{
    //  //base.Init += new EventHandler(this.EditDate_Init);
    //  //base.Load += new EventHandler(this.Page_Load);
    //}




    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      //base.Init += new EventHandler(this.EditDate_Init);
      InitEditDate();
    }




    //protected void Page_Load(object sender, EventArgs evt)
    protected override void OnLoad(EventArgs evt)
    {
      base.OnLoad(evt);

      if (this.ctlValueBox is TextBox)
        this.ctlHyp.NavigateUrl = DotNetNuke.Common.Utilities.Calendar.InvokePopupCal((TextBox)this.ctlValueBox);
      
    }




    //private void EditDate_Init(object sender, EventArgs evt)
    private void InitEditDate()
    {
      if (this.IsNotAListOfValues)
      {
        TextBox box2 = new TextBox();
        Unit unit = new Unit("7em");
        box2.Width = unit;
        TextBox child = box2;
        child.Attributes.Add("style", "text-align:right");
        this.ctlHyp = new HyperLink();
        this.ctlHyp.ID = EditControl.CleanID(string.Format("{0}_hyp", this.FieldTitle));
        this.ctlHyp.Attributes.Add("resourcekey", "Calendar");
        this.ctlHyp.Text = "Calendar";
        this.ctlHyp.CssClass = "CommandButton";
        child.CssClass = "NormalTextBox";
        if (!string.IsNullOrEmpty(this.Style))
        {
          child.Style.Value = this.Style;
        }
        child.ID = EditControl.CleanID(string.Format("{0}_date", this.FieldTitle));
        this.Controls.Add(child);
        this.Controls.Add(new LiteralControl("&nbsp;"));
        this.Controls.Add(this.ctlHyp);
        this.ctlValueBox = child;
      }
      else
      {
        ListControl control;
        switch (this.ListInputType)
        {
          case EditControl.InputType.horizontalRadioButtons:
            control = new RadioButtonList();
            ((RadioButtonList)control).RepeatDirection = RepeatDirection.Horizontal;
            break;

          case EditControl.InputType.verticalRadioButtons:
            control = new RadioButtonList();
            break;

          default:
            control = new DropDownList();
            break;
        }
        DateTime tm;
        foreach (string str in this.InputValueList)
        {
          if (DateTime.TryParse(str, out tm))
          {
            //DateTime time = DateTime.Parse(str.Trim());
            control.Items.Add(new ListItem(tm.ToString("d"), tm.ToString("s")));
          }
        }
        if (!this.Required)
          control.Items.Add(new ListItem("", ""));

        control.CssClass = "NormalTextBox";
        control.Style.Value = this.Style;
        control.ID = EditControl.CleanID(string.Format("{0}_date", this.FieldTitle));
        this.Controls.Add(control);
        this.ctlValueBox = control;
      }
      this.Value = this.DefaultValue;
    }





    protected override bool IsValidType()
    {
      if (this.ctlValueBox is TextBox)
      {
        bool flag;
        string objField = ((TextBox)this.ctlValueBox).Text.Trim();

        if (Null.IsNull(objField))
          return true;
        
        try
        {
          DateTime.ParseExact(objField, new string[] { "d", "D" }, Thread.CurrentThread.CurrentCulture, DateTimeStyles.None);
          flag = true;
        }
        catch (Exception e)
        {
          Exception exception = e;
          flag = false;
        }
        return flag;
      }
      return true;
    }







    public override string Value
    {
      get
      {
        string text;
        if (this.ctlValueBox is TextBox)
          text = ((TextBox)this.ctlValueBox).Text;
        
        else
        
          text = ((DropDownList)this.ctlValueBox).SelectedValue;
        if ( string.IsNullOrEmpty(text) )
          return Null.NullString;
        
        DateTime tmp;
        if (DateTime.TryParse(text, out tmp))
          text = tmp.ToString("s");

        return text;
      }
      set
      {
        DateTime time;
        if (DateTime.TryParse(value, out time))
        {
          //DateTime time = DateTime.Parse(value);
          if (this.ctlValueBox is TextBox)
            ((TextBox)this.ctlValueBox).Text = time.ToString("d");

          else
          {
            ListControl ctlValueBox = (ListControl)this.ctlValueBox;
            if (ctlValueBox.Items.FindByValue(time.ToString("s")) != null)
              ctlValueBox.SelectedValue = time.ToString("s");

          }
        }
        else if (this.ctlValueBox is TextBox)
          ((TextBox)this.ctlValueBox).Text = string.Empty;

        else
        {
          ListControl control2 = (ListControl)this.ctlValueBox;
          if (this.Required)
            control2.SelectedIndex = 0;

          else
            control2.SelectedValue = "";

        }
      }
    }



  }

}

